package com.example.mycomposelearing

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.mycomposelearing.bean.User
import com.example.mycomposelearing.ui.theme.MyComposeLearingTheme


/**
 *基础布局位置测试
 */
class BaseLayoutMainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyComposeLearingTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Greeting2("Android")
                }
            }
        }
    }
}

@Composable
fun Greeting2(name: String, modifier: Modifier = Modifier) {
    val scrollState = rememberScrollState()
    Column(
        modifier = Modifier
            .fillMaxSize()
            .verticalScroll(scrollState)
    ) {
        //
        PhotographerCard()
        BoxColumn()
    }
}



// compositionLocalOf 方法可以创建一个 CompositionLocal 实例
val ActiveUser = compositionLocalOf {
    // 设置默认值
    User().apply {
        name = "小明"
        time = "3分钟"
    }
    // 如果无须默认值，也可设置错误信息
//    error("No active user found!")
}

// 跨compose内容传参
@Composable
private fun PhotographerCard() {

    Row(
        modifier = Modifier
            .fillMaxWidth()  // 相当于 width = match_parent
            .padding(10.dp)  // 外边距为 10dp
            .clip(RoundedCornerShape(6.dp))  // 设置圆角
            .clickable { }  // 点击事件
            .padding(16.dp)  // 内边距为 16dp
    ) {
        Surface(
            modifier = Modifier.size(50.dp),  // 设置大小
            shape = CircleShape,  // 设置形状
            color = Color(0xFF1D1D1D)
        ) {
            // 加载网络图片逻辑

        }

        Column(
            modifier = Modifier
                .padding(start = 8.dp)  // 单独设置 左边距
                .align(Alignment.CenterVertically)  // 设置里面的子元素竖直方向上居中分布
        ) {
            val user = ActiveUser.current // 通过 current 方法取出当前值
            Text(user?.name ?: "", fontWeight = FontWeight.Bold)
            user.name = "小明改值为小红"
            CompositionLocalProvider(ActiveUser provides user) {
                Text(user?.time ?: "")
            }
        }
    }

}

@Composable
private fun RowScope.PhotographerCard1() {

        Column(
            modifier = Modifier
                .padding(start = 8.dp)  // 单独设置 左边距
                .align(Alignment.CenterVertically)  // 设置里面的子元素竖直方向上居中分布
        ) {
            val user = ActiveUser.current // 通过 current 方法取出当前值
            Text(user?.name ?: "", fontWeight = FontWeight.Bold)
            CompositionLocalProvider(ActiveUser provides user) {
                Text(user?.time ?: "")
            }
        }

}



/**
 *模拟使用box写一个clumn布局 代码应该是有问题的
 */
@Composable
fun BoxColumn() {

    Box {
        var imageHeightPx by remember { mutableStateOf(0) }

        Image(
            painter = painterResource(R.drawable.ic_launcher_background),
            contentDescription = "I'm above the text",
            modifier = Modifier
                .fillMaxWidth()
                .onSizeChanged { size ->
                    // Don't do this
                    imageHeightPx = size.height
                }
        )

        Text(
            text = "I'm below the image",
            modifier = Modifier.padding(
                top = with(LocalDensity.current) { imageHeightPx.toDp() }
            )
        )
    }
}


@Preview(showBackground = true)
@Composable
fun GreetingPreview2() {
    MyComposeLearingTheme {
        Greeting2("Android")
    }
}